package com.peek.controller;

import com.google.common.collect.Lists;
import com.jfinal.kit.HttpKit;
import com.peek.bean.ServerConfig;
import com.peek.dao.ServerConfigRepository;
import com.xiaoleilu.hutool.system.OsInfo;
import com.xiaoleilu.hutool.system.SystemUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

	private static Logger log = LoggerFactory.getLogger(IndexController.class);

	private static OsInfo osInfo = SystemUtil.getOsInfo();

	@Autowired
	ServerConfigRepository serverConfigRepository;

	@RequestMapping("/")
	public String index(HttpServletRequest request,Model model){

		log.info("index 首页");

		List<ServerConfig> list = serverConfigRepository.findAll();

		List<List<ServerConfig>> result = Lists.partition(list, 3);

		model.addAttribute("result",result);

		return "index";
	}



	@RequestMapping("/index2")
	public String index2(Integer id,Model model){

		model.addAttribute("id",id);

		return "index2";
	}


	@RequestMapping("/view")
	public String view(String dirId,String url,Model model){

		long time1 = System.currentTimeMillis();

		model.addAttribute("dirId",dirId);
		model.addAttribute("url",url);
		try {
			url = java.net.URLDecoder.decode(url, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String content = HttpKit.get(url+"/meng/file/view?dirId="+dirId);

		model.addAttribute("content",content);

		long time2 = System.currentTimeMillis();

		log.info("查看文件耗时 --> "+(time2-time1)+" (ms)");

		return "view";
	}


	@RequestMapping("/content")
	@ResponseBody
	public String content(String dirId,String content,String url){
		long time1 = System.currentTimeMillis();
		try {
			url = java.net.URLDecoder.decode(url, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		Map<String,String> map = new HashMap<>();
		map.put("dirId",dirId);
		map.put("content",content);

		HttpKit.post(url+"/meng/file/content",map,"");

		long time2 = System.currentTimeMillis();

		log.info("保存文件耗时 --> "+(time2-time1)+" (ms)");

		return "ok";
	}


	@RequestMapping("/switchFlag")
	public String switchFlag(Integer flag,Integer id,Model model){
		String page = "file";

		if (flag==2){
			page = "database";
		}

		ServerConfig config = serverConfigRepository.getOne(id);

		model.addAttribute("url",config.getUrl());

		return page;
	}





	@RequestMapping("/addServer")
	@ResponseBody
	public Boolean addServer(ServerConfig serverConfig){

		boolean flag = false;

		try {
			serverConfigRepository.save(serverConfig);
			flag = true;
		}catch (Exception e){
			e.printStackTrace();
			log.error("添加服务配置失败！",e);
		}

		return flag;
	}




	@RequestMapping("/delServer")
	@ResponseBody
	public Boolean delServer(Integer id){

		boolean flag = false;

		try {
			serverConfigRepository.delete(id);
			flag = true;
		}catch (Exception e){
			e.printStackTrace();
			log.error("删除服务配置失败！",e);
		}

		return flag;
	}






}



















